<?php

/**
 * This is the model class for table "{{post}}".
 *
 * The followings are the available columns in table '{{post}}':
 * @property string $pid
 * @property string $uid
 * @property string $gid
 * @property string $cid
 * @property string $type
 * @property string $forward_pid
 * @property string $forward_root_pid
 * @property string $title
 * @property string $vt_content
 * @property string $reply_num
 * @property string $forward_num
 * @property string $config
 * @property string $priority
 * @property string $create_time
 * @property string $last_modify_time
 * @property string $status
 */
class PostData extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @return PostDao the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{post}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('uid, vt_content, create_time, last_modify_time', 'required'),
            array('uid, gid, cid, type, forward_pid, forward_root_pid, vt_content, reply_num, forward_num, config, priority, create_time, last_modify_time, status', 'length', 'max'=>10),
            array('title', 'length', 'max'=>50),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('pid, uid, gid, cid, type, forward_pid, forward_root_pid, title, vt_content, reply_num, forward_num, config, priority, create_time, last_modify_time, status', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'pid' => 'Pid',
            'uid' => 'Uid',
            'gid' => 'Gid',
            'cid' => 'Cid',
            'type' => 'Type',
            'forward_pid' => 'Forward Pid',
            'forward_root_pid' => 'Forward Root Pid',
            'title' => 'Title',
            'vt_content' => 'Vt Content',
            'reply_num' => 'Reply Num',
            'forward_num' => 'Forward Num',
            'config' => 'Config',
            'priority' => 'Priority',
            'create_time' => 'Create Time',
            'last_modify_time' => 'Last Modify Time',
            'status' => 'Status',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('pid',$this->pid,true);
        $criteria->compare('uid',$this->uid,true);
        $criteria->compare('gid',$this->gid,true);
        $criteria->compare('cid',$this->cid,true);
        $criteria->compare('type',$this->type,true);
        $criteria->compare('forward_pid',$this->forward_pid,true);
        $criteria->compare('forward_root_pid',$this->forward_root_pid,true);
        $criteria->compare('title',$this->title,true);
        $criteria->compare('vt_content',$this->vt_content,true);
        $criteria->compare('reply_num',$this->reply_num,true);
        $criteria->compare('forward_num',$this->forward_num,true);
        $criteria->compare('config',$this->config,true);
        $criteria->compare('priority',$this->priority,true);
        $criteria->compare('create_time',$this->create_time,true);
        $criteria->compare('last_modify_time',$this->last_modify_time,true);
        $criteria->compare('status',$this->status,true);

        return new CActiveDataProvider(get_class($this), array(
            'criteria'=>$criteria,
        ));
    }
    
    public function editPost($content, $title, $config)
    {
        $this->vt_content = $content;
        $this->title = $title;
        $this->config = $config;
        $this->last_modify_time = time();
        
        $this->setIsNewRecord(false);
        $update_attributes[] = array('last_modify_time', 'config', 'title', 'vt_content');
        $this->setIsNewRecord(false);
        $res = $this->update($update_attributes);
        if (false === $res) {
            throw new Exception('error in updating data for post[' . $this->pid . ']' , ErrorDef::COMMON_ERROR_SYS);
        }
    }
    
    public static function addPost($title, $content, $type, $uid, $gid, $category_id, $config)
    {
        $post = new PostData();
        if (empty($title))
        {
            $title = '';
        }
        $post->title = $title;
        $post->vt_content = $content;
        $post->type = $type;
        $post->uid = $uid;
        $post->gid = $gid;
        $post->cid = $category_id;
        $post->config = $config;
        $post->status = PostStatusDef::NORMAL;
        
        $time  = time();
        $post->create_time = $time;
        $post->last_modify_time = $time;
        $res = $post->inesrt();
        if (false === $res)
        {
            throw new Exception('error in insert data to PostData', ErrorDef::COMMON_ERROR_SYS);
        }
    }
}
